package de.breitbeil.jbooking.ui.components;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.util.Calendar;
import java.util.Date;

import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;

import org.springframework.richclient.command.ActionCommand;


public class YearBookingCalendar extends JPanel {

    private static String DEFAULT_LABEL = "Year";
    
    private BookingCalendarComponent monthView;
    private JLabel yearLabel;

    private String yearLabelText = DEFAULT_LABEL;
    
    public YearBookingCalendar() {
        monthView = new BookingCalendarComponent();
        
        monthView.setFirstDayOfWeek(Calendar.MONDAY);
        monthView.setFirstDisplayedDay(getFirstDayOfYear());
        monthView.setPreferredCols(3);
        monthView.setPreferredRows(4);
        monthView.setMaximumSize(monthView.getPreferredSize());
        
        JPanel yearControlBorder = new JPanel();
        yearControlBorder.setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 0));
        yearControlBorder.setBackground(null);
        yearControlBorder.add(createYearControl());
        
        setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
        add(yearControlBorder);
        add(monthView);
        setBackground(Color.WHITE);
    }
    
    public BookingCalendarComponent getMonthView() {
        return monthView;
    }
    
    public void setYearLabelText(String yearLabelText) {
        this.yearLabelText  = yearLabelText;
    }

    public void setFirstDisplayedDay(Calendar cal) {
    	getMonthView().setFirstDisplayedDay(cal.getTime());
    	yearLabel.setText(getLabel(cal.get(Calendar.YEAR)));
    }
    
    private Date getFirstDayOfYear() {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH, Calendar.JANUARY);
        return calendar.getTime();
    }
    
    private JComponent createYearControl() {
        Calendar cal = Calendar.getInstance();
        cal.setTime(monthView.getFirstDisplayedDay());
        yearLabel = new JLabel( getLabel(  cal.get(Calendar.YEAR) ));
        yearLabel.setHorizontalAlignment(JLabel.CENTER);
        Font font = monthView.getFont().deriveFont(Font.BOLD);
        yearLabel.setFont(font);
        Icon monthDownImage = UIManager.getIcon("JXMonthView.monthDownFileName");
        Icon monthUpImage = UIManager.getIcon("JXMonthView.monthUpFileName");
        
        BrowseYearCommand prevCommand = new BrowseYearCommand(-1, monthView, yearLabel);
        BrowseYearCommand nextCommand = new BrowseYearCommand(1, monthView, yearLabel);
      
        AbstractButton prevButton = prevCommand.createButton();
        AbstractButton nextButton = nextCommand.createButton();
        prevButton.setBackground(null);
        prevButton.setBorder(null);
        nextButton.setBackground(null);
        nextButton.setBorder(null);
        prevButton.setIcon(monthDownImage);
        nextButton.setIcon(monthUpImage);
        
        JPanel panel = new JPanel(new BorderLayout());
        panel.add(yearLabel, BorderLayout.CENTER);
        panel.add(prevButton, BorderLayout.WEST);
        panel.add(nextButton, BorderLayout.EAST);
        
        panel.setBackground(monthView.getMonthStringBackground());
        panel.setForeground(monthView.getMonthStringForeground());
        Dimension preferredSize = new Dimension((int)monthView.getPreferredSize().getWidth(), 30);
        panel.setPreferredSize(preferredSize);
        panel.setMaximumSize(preferredSize);
        
        return panel;
    }
    
    protected String getLabel(int year) {
        return yearLabelText + " " +year;
    }
    
    protected class BrowseYearCommand extends ActionCommand {
        
        private int offset;
        private BookingCalendarComponent calendar;
        private JLabel yearLabel;
        
        public BrowseYearCommand(int offset, BookingCalendarComponent calendar, JLabel yearLabel) {
            this.offset = offset;
            this.calendar = calendar;
            this.yearLabel = yearLabel;
        }
        
        @Override
        public void doExecuteCommand() {
            Date oldDate = calendar.getFirstDisplayedDay();
            Calendar cal = Calendar.getInstance();
            cal.setTime(oldDate);
            cal.add(Calendar.YEAR, offset);
            setFirstDisplayedDay(cal);
        }
    }
}
